﻿using Python.Deployment;
using Python.Runtime;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CallPythonDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            PyIncludedTest();

            Console.ReadKey();
        }


        /// <summary>
        /// Python.Included库调用python,需要nuget安装Python.Included
        /// </summary>
        static async void PyIncludedTest()
        {
            //设置Python环境安装路径
            Installer.InstallPath = Path.GetFullPath(".");

            //注册显示安装日志信息
            Installer.LogMessage += Console.WriteLine;

            //执行Python环境安装（如果发现已经安装会跳过）
            await Installer.SetupPython();

            //安装pip3
            Installer.TryInstallPip();

            //用pip安装第三方模块
            Installer.PipInstallModule("urllib3");

            PythonEngine.Initialize();

            //获取一个模块或者包作为对象返回
            dynamic sys = PythonEngine.ImportModule("sys");
            Console.WriteLine("Python version: " + sys.version);
            
            //调用urllib3模块
            //dynamic urllib3 = PythonEngine.ImportModule("urllib3");
            
            //调用自己写的test模块
            dynamic test = PythonEngine.ImportModule("test");
            Console.WriteLine(test.Add(3, 7));

            //调用自己写的test2模块
            dynamic test2 = PythonEngine.ImportModule("test2");
            var res = test2.GetUrl("https://api.apishop.net/common/postcode/queryAddrByAreaCode");
            Console.WriteLine(res.data);

            //第二种调用方式
            test2 = Py.Import("test2");
            Console.WriteLine(test2.welcome("小明"));

            //第二种调用方式
            var hw = Py.Import("helloworld");

        }
    }
}
